package com.hotelsystem.reservations.layouts;

import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.*;

import com.hotelsystem.reservation.controllers.SaveUserController;
import com.hotelsystem.reservations.persons.Client;
import com.hotelsystem.reservations.reservations.ReservationDetails;

public class OpenUserView {

Client client;
JLabel imageLabel;
JButton btnChoseImage;



private ActionListener choseImageListner=new ActionListener() {	
	@Override
	public void actionPerformed(ActionEvent e) {		
		JFileChooser chooser = new JFileChooser();
		if( chooser.showOpenDialog(mainPanel)==0){
			client.setImageFilePath(chooser.getSelectedFile().getPath());
			viewPicture();
		}
		
	}
};


private void viewPicture(){

	String pth = client.getImageFilePath();
	if(pth==null)pth="";
	File imgFile = new File(pth);
	if(!imgFile.exists()){
		imgFile=new File("Pictures","default_photo.png");
	}
	Icon icon = new ImageIcon(imgFile.getAbsolutePath());
	imageLabel.setIcon(icon);
}

public JPanel getPanel(){
	return mainPanel;
}
public OpenUserView(Client client,boolean editable) {
	super();
	this.client = client;
	this.editable=editable;
	if(client==null)return;
	buildUI();
}


    JTextField tfFirstName;
    JTextField tfLastName;
    JTextField tfLanguage;
    JTextField tfEmail;
    JTextField tfAge;
    JTextField tfPersonalId;
    JTextField tfTelephone;
    JRadioButton male;
    JRadioButton female;
    JTextField tfAdditionalBills;


    JPanel mainPanel;
    boolean editable;
    JDialog resDetFrame = new JDialog();
    private ActionListener showResDetListener=new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createEmptyBorder(15,15,15,15));
            GridLayout layout = new GridLayout();
            panel.setLayout(layout);
            layout.setColumns(2);
            layout.setRows(0);
            layout.setHgap(15);
            layout.setVgap(15);

            JLabel lblSmokersFree=new JLabel("SmokersFree");
            panel.add(lblSmokersFree);
            JPanel p1 = new JPanel(new FlowLayout());
            ButtonGroup bg = new ButtonGroup();
            final JRadioButton y_smo = new JRadioButton("Yes");
            final JRadioButton n_smo = new JRadioButton("No");
            if(editable) {
                bg.add(y_smo);
                bg.add(n_smo);
                p1.add(y_smo);
                p1.add(n_smo);
                panel.add(p1);
            }
            else {
                String s="";
                if (client.getReservationDetails().isSmokersFree() != null) {
                    if(client.getReservationDetails().isSmokersFree()){
                        s="Yes";
                    }
                    else{s="No";}
                }
                panel.add(new JLabel(s));
            }

            JLabel lblHasDinner=new JLabel("HasDinner");
            panel.add(lblHasDinner);
            p1 = new JPanel(new FlowLayout());
            bg = new ButtonGroup();
            final JRadioButton y_din = new JRadioButton("Yes");
            final JRadioButton n_din = new JRadioButton("No");
            if(editable) {
                bg.add(y_din);
                bg.add(n_din);
                p1.add(y_din);
                p1.add(n_din);
                panel.add(p1);
            }else {
                String s="";
                if (client.getReservationDetails().isHasDinner() != null) {
                    if(client.getReservationDetails().isHasDinner()){
                        s="Yes";
                    }
                    else{s="No";}
                }
                panel.add(new JLabel(s));
            }

            JLabel lblHasBreakfast=new JLabel("HasBreakfast");
            panel.add(lblHasBreakfast);
            p1 = new JPanel(new FlowLayout());
            bg = new ButtonGroup();
            final JRadioButton y_br = new JRadioButton("Yes");
            final JRadioButton n_br = new JRadioButton("No");
            if(editable) {
                bg.add(y_br);
                bg.add(n_br);
                p1.add(y_br);
                p1.add(n_br);
                panel.add(p1);
            }else {
                String s="";
                if (client.getReservationDetails().isHasBreakfast() != null) {
                    if(client.getReservationDetails().isHasBreakfast()){
                        s="Yes";
                    }
                    else{s="No";}
                }
                panel.add(new JLabel(s));
            }


            JButton save_button = new JButton("Save");
            save_button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ReservationDetails buf = new ReservationDetails();
                    Boolean temp;
                    if (y_smo.isSelected() || n_smo.isSelected()) {
                        temp = (y_smo.isSelected()) ? true : false;
                        buf.setSmokersFree(temp);
                    }
                    if (y_br.isSelected() || n_br.isSelected()) {
                        temp = (y_br.isSelected()) ? true : false;
                        buf.setHasBreakfast(temp);
                    }
                    if (y_din.isSelected() || n_din.isSelected()) {
                        temp = (y_din.isSelected()) ? true : false;
                        buf.setHasDinner(temp);
                    }
                    client.setReservationDetails(buf);
                   // System.out.println(buf.toString());
                    resDetFrame.dispose();
                }
            });
            save_button.setEnabled(editable);
            panel.add(new JLabel());
            panel.add(save_button);

            resDetFrame.add(panel);
            resDetFrame.setSize(new Dimension(320,240));
            resDetFrame.setPreferredSize(new Dimension(320,240));
            System.out.println(client.toString());
            resDetFrame.setTitle(client.getFirstName()+" "+client.getLastName()+" Reservation Details");
            resDetFrame.setVisible(true);
            resDetFrame.setModal(true);
            resDetFrame.setModalityType(ModalityType.APPLICATION_MODAL);

        }
    };

    public Client getClient() {
        return client;
    }

    public JTextField getTfFirstName() {
        return tfFirstName;
    }

    public JTextField getTfLastName() {
        return tfLastName;
    }

    public JTextField getTfLanguage() {
        return tfLanguage;
    }

    public JTextField getTfEmail() {
        return tfEmail;
    }

    public JTextField getTfAge() {
        return tfAge;
    }

    public JTextField getTfPersonalId() {
        return tfPersonalId;
    }

    public JTextField getTfTelephone() {
        return tfTelephone;
    }

    public JRadioButton getMale() {
        return male;
    }

    public JRadioButton getFemale() {
        return female;
    }

    public JTextField getTfAdditionalBills() {
        return tfAdditionalBills;
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    public boolean isEditable() {
        return editable;
    }

    public JDialog getResDetFrame() {
        return resDetFrame;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    /*public OpenUserView(){
        this.client = new Client();
        this.editable = true;
        buildUI();
    }*/

   
    private void buildUI() {
        mainPanel=new JPanel();
        JPanel panel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setSize(new Dimension(815,515));
        mainPanel.setPreferredSize(new Dimension(815,515));
        mainPanel.setMinimumSize(new Dimension(815,286));
        JPanel imagePanel =new JPanel();
    	imagePanel.setLayout(new BorderLayout());
    	imagePanel.setSize(new Dimension(256,286));
    	imagePanel.setPreferredSize(new Dimension(256,286));
    	imagePanel.setMaximumSize(new Dimension(256,286));
        imageLabel = new JLabel("");
        btnChoseImage = new JButton("Choose Image");
        viewPicture();
        
        
        JPanel imageLabelPanel =new JPanel();

        imageLabelPanel.add(imageLabel);
        imagePanel.add(imageLabelPanel,BorderLayout.CENTER);
               
       
        
        btnChoseImage.setEnabled(editable);
        btnChoseImage.addActionListener(choseImageListner);
        JPanel choseImageBtnPanel =new JPanel();
        choseImageBtnPanel.add(btnChoseImage);
        imagePanel.add(choseImageBtnPanel,BorderLayout.SOUTH);
    	
    	mainPanel.add(imagePanel,BorderLayout.EAST);
        mainPanel.add(panel,BorderLayout.WEST);
        GridLayout layout = new GridLayout();
        panel.setLayout(layout);
        layout.setColumns(2);
        layout.setRows(0);
        layout.setHgap(10);
        layout.setVgap(10);
        panel.setPreferredSize(new Dimension(360,480));
        panel.setMinimumSize(new Dimension(360,480));
        panel.setSize(new Dimension(360,480));
        panel.setBorder(BorderFactory.createEmptyBorder(15,15,15,15));
        JLabel lblFirstName=new JLabel("First Name:");
        panel.add(lblFirstName);
        tfFirstName = new JTextField(client.getFirstName());
        tfFirstName.setEditable(editable);
        panel.add(tfFirstName);

        JLabel lblLastName=new JLabel("Last Name:");
        panel.add(lblLastName);
        tfLastName = new JTextField(client.getLastName());
        tfLastName.setEditable(editable);
        panel.add(tfLastName);

        JLabel lblLanguage=new JLabel("Language:");
        panel.add(lblLanguage);
        tfLanguage = new JTextField(client.getLanguage());
        tfLanguage.setEditable(editable);
        panel.add(tfLanguage);

        JLabel lblEmail=new JLabel("Email:");
        panel.add(lblEmail);
        tfEmail = new JTextField(client.getEmail());
        tfEmail.setEditable(editable);
        panel.add(tfEmail);

        JLabel lblAge=new JLabel("Age:");
        panel.add(lblAge);
        JPanel p1 = new JPanel(new GridLayout(0,2));
        tfAge = new JTextField(client.getAge()+"");
        tfAge.setEditable(editable);
        p1.add(tfAge);
        p1.add(new JLabel(" Years"));
        panel.add(p1);

        JLabel lblPersonalId=new JLabel("PersonalId:");
        panel.add(lblPersonalId);
        tfPersonalId = new JTextField(client.getPersonalId());
        tfPersonalId.setEditable(editable);
        panel.add(tfPersonalId);

        JLabel lblTelephone=new JLabel("Telephone:");
        panel.add(lblTelephone);
        tfTelephone = new JTextField(client.getTelephone());
        tfTelephone.setEditable(editable);
        panel.add(tfTelephone);

        JLabel lblAdditionalBills=new JLabel("AdditionalBills:");
        panel.add(lblAdditionalBills);
        p1 = new JPanel(new GridLayout(0,2));
        tfAdditionalBills = new JTextField(client.getAdditionalBills()+"");
        tfAdditionalBills.setEditable(editable);
        p1.add(tfAdditionalBills);
        p1.add(new JLabel("$"));
        panel.add(p1);

        JLabel lblGender=new JLabel("Gender:");
        panel.add(lblGender);
        if(editable) {
            p1 = new JPanel(new FlowLayout());
            ButtonGroup bg = new ButtonGroup();
            male = new JRadioButton("Male");
            female = new JRadioButton("Female");
            bg.add(male);
            bg.add(female);
            p1.add(male);
            p1.add(female);
            panel.add(p1);
        }
        else{
            panel.add(new JLabel(client.getGender().toString()));
        }

        JLabel lblReservationDetails=new JLabel("ReservationDetails:");
        panel.add(lblReservationDetails);
        JButton showReservationDetails = new JButton((editable)?"Set Reservation Details":"Show Reservation Details");
        if(!editable && client.getReservationDetails() == null){
            showReservationDetails.setEnabled(false);
        }
        else{
            showReservationDetails.setEnabled(true);
        }
        showReservationDetails.addActionListener(showResDetListener);
        panel.add(showReservationDetails);
        JButton save_button = new JButton("Save");
        save_button.addActionListener(new SaveUserController(this));
        save_button.setEnabled(editable);
        panel.add(new JLabel());
        panel.add(save_button);
    }
//    private void buildUI() {
//    	mainPanel=new JPanel();
//    	JPanel panel = new JPanel();
//    	mainPanel.setLayout(new BorderLayout());
//    	mainPanel.setSize(512,735);
//    	mainPanel.setPreferredSize(new Dimension(515,735));
//    	mainPanel.setMinimumSize(new Dimension(515,735));
//    	JPanel imagePanel =new JPanel();
//    	imagePanel.setLayout(new BorderLayout());
//    	imagePanel.setSize(new Dimension(515,286));
//    	imagePanel.setPreferredSize(new Dimension(515,286));
//    	imagePanel.setMaximumSize(new Dimension(515,286));
//        imageLabel = new JLabel("");
//        btnChoseImage = new JButton("Choose Image");
//        viewPicture();
//        
//        
//        JPanel imageLabelPanel =new JPanel();
//
//        imageLabelPanel.add(imageLabel);
//        imagePanel.add(imageLabelPanel,BorderLayout.CENTER);
//        
//        
//       
//        
//        btnChoseImage.setEnabled(editable);
//        btnChoseImage.addActionListener(choseImageListner);
//        JPanel choseImageBtnPanel =new JPanel();
//        choseImageBtnPanel.add(btnChoseImage);
//        imagePanel.add(choseImageBtnPanel,BorderLayout.SOUTH);
//    	
//    	mainPanel.add(imagePanel,BorderLayout.NORTH);
//    	mainPanel.add(panel,BorderLayout.SOUTH);
//    	GridLayout layout = new GridLayout();
//    	
//    	panel.setLayout(layout);
//    	layout.setColumns(2);
//    	layout.setRows(0);
//    	panel.setPreferredSize(new Dimension(300,300));
//    	panel.setSize(new Dimension(300,300));
//    	panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
//    	JLabel lblFirstName=new JLabel("First Name:");	
//    	panel.add(lblFirstName);
//    	JTextField tfFirstName = new JTextField(client.getFirstName());
//    	tfFirstName.setEditable(editable);
//    	panel.add(tfFirstName);
//    	
//    	JLabel lblLastName=new JLabel("Last Name:");	
//    	panel.add(lblLastName);
//    	JTextField tfLastName = new JTextField(client.getLastName());
//    	tfLastName.setEditable(editable);
//    	panel.add(tfLastName);
//    	
//    	JLabel lblLanguage=new JLabel("Language:");	
//    	panel.add(lblLanguage);
//    	JTextField tfLanguage = new JTextField(client.getLanguage());
//    	tfLanguage.setEditable(editable);
//    	panel.add(tfLanguage);
//    	
//    	JLabel lblEmail=new JLabel("Email:");	
//    	panel.add(lblEmail);
//    	JTextField tfEmail = new JTextField(client.getEmail());
//    	tfEmail.setEditable(editable);
//    	panel.add(tfEmail);
//    	
//    	JLabel lblAge=new JLabel("Age:");	
//    	panel.add(lblAge);
//    	JTextField tfAge = new JTextField(client.getAge()+" Years");
//    	tfAge.setEditable(editable);
//    	panel.add(tfAge);
//    	
//    	JLabel lblPersonalId=new JLabel("PersonalId:");	
//    	panel.add(lblPersonalId);
//    	JTextField tfPersonalId = new JTextField(client.getPersonalId());
//    	tfPersonalId.setEditable(editable);
//    	panel.add(tfPersonalId);
//    	
//    	JLabel lblTelephone=new JLabel("Telephone:");	
//    	panel.add(lblTelephone);
//    	JTextField tfTelephone = new JTextField(client.getTelephone());
//    	tfTelephone.setEditable(editable);
//    	panel.add(tfTelephone);
//    	
//    	JLabel lblAdditionalBills=new JLabel("AdditionalBills:");	
//    	panel.add(lblAdditionalBills);
//    	JTextField tfAdditionalBills = new JTextField(client.getAdditionalBills()+"$");
//    	tfAdditionalBills.setEditable(editable);
//    	panel.add(tfAdditionalBills);
//    	
//    	JLabel lblGender=new JLabel("Gender:");	
//    	panel.add(lblGender);
//    	JTextField tfGender = new JTextField(client.getGender()+"");
//    	tfGender.setEditable(editable);
//    	panel.add(tfGender);
//    	
//    	JLabel lblReservationDetails=new JLabel("ReservationDetails:");	
//    	panel.add(lblReservationDetails);
//    	JButton showReservationDetails = new JButton("Show Reservation Details");
//    	showReservationDetails.setEnabled(client.getReservationDetails()!=null);
//    	showReservationDetails.addActionListener(showResDetListener);
//    	panel.add(showReservationDetails);
//    	
//    }



}
